package com.jytc.framework.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.jytc.framework.bo.FwDeptBO;
import com.jytc.framework.bo.FwRoleBO;
import com.jytc.framework.bo.FwUserBO;
import com.jytc.framework.commons.AdminLoginToken;
import com.jytc.framework.commons.Page;
import com.jytc.framework.commons.PropertyFilter;
import com.jytc.framework.commons.ResultInfos;
import com.jytc.framework.constants.CommonConstants;
import com.jytc.framework.constants.RequestNameConstants;
import com.jytc.framework.constants.ResultConstants;
import com.jytc.framework.constants.SessionNameConstants;
import com.jytc.framework.constants.UrlConstants;
import com.jytc.framework.dto.FwUserDTO;
import com.jytc.framework.po.FwDept;
import com.jytc.framework.po.FwRole;
import com.jytc.framework.utils.HibernateWebUtils;
import com.jytc.framework.utils.SessionUtils;

/**
 * @title: FSysUserController.java
// * @description: 角色管理
 * 
 * @author: dongyuese
 *
 */
@Controller
@RequestMapping("/admin/jsp/userMgr.do")
public class FwUserController
{
	protected Logger logger = LoggerFactory.getLogger(getClass());//日志

	/**
	 * 查询展示页面
	 */
	private final String QUERY_GOTO_URL = "/admin/jsp/org_mgr/user_mgr/user_mgr.jsp";

	/**
	 * 编辑页面
	 */
	private final String EDIT_GOTO_URL = "/admin/jsp/org_mgr/user_mgr/user_edit.jsp";

	/**
	 * 增加页面
	 */
	private final String ADD_GOTO_URL = "/admin/jsp/org_mgr/user_mgr/user_add.jsp";

	/**
	 * 查询URL
	 */
	private final String VIEW_URL = "/admin/jsp/userMgr.do?method=queryUser";

	/**
	 * 新增URL
	 */
	private final String ADD_URL = "/admin/jsp/userMgr.do?method=addUser";

	/**
	 * 编辑URL
	 */
	private final String EDIT_URL = "/admin/jsp/userMgr.do?method=editUser";

	/**
	 * 用户管理业务操作对象
	 */
	@Autowired
	private FwUserBO fwUserBO;

	/**
	 * 单位管理业务操作对象
	 */
	@Autowired
	private FwDeptBO fwDeptBO;

	/**
	 * 角色管理业务操作对象
	 */
	@Autowired
	private FwRoleBO fwRoleBO;

	/**
	 * 用户信息查询
	 * @param request　请求对象
	 * @param model
	 * @return
	 */
	@RequestMapping(params = "method=queryUser")
	public ModelAndView queryUser(HttpServletRequest request, ModelMap model, Page page)
	{
		ResultInfos resultInfos = new ResultInfos();
		model.addAttribute(RequestNameConstants.RESULT_INFO, resultInfos);
		try
		{
			//获取管理系统类型
			String systemType = request.getParameter(RequestNameConstants.SYSTEM_TYPE);

			List<FwDept> corpList = fwDeptBO.findByCorpType("corpType", systemType);
			model.addAttribute("corpList", corpList);

			List<PropertyFilter> filters = HibernateWebUtils.buildPropertyFilters(request);
			Page pageResult = fwUserBO.query(page, filters);

			model.addAttribute(RequestNameConstants.PAGE_OBJECT, pageResult);
			model.addAttribute(RequestNameConstants.SYSTEM_TYPE, systemType);
		} catch (Exception e)
		{
			logger.error("用户查询异常", e);

			// 显示提示信息
			resultInfos.setPromptInfo(ResultConstants.getResultInfo(ResultConstants.QUERY_ERROR));
			resultInfos.setIsBack(false);
			resultInfos.setIsRedirect(false);
			return new ModelAndView(UrlConstants.INFORMATION_PAGE);
		}
		return new ModelAndView(QUERY_GOTO_URL);
	}

	/**
	 * 用户新增页面转向方法
	 * @param request 请求对象
	 * @param model
	 * @return
	 */
	@RequestMapping(params = "method=addUser")
	public ModelAndView addUser(HttpServletRequest request, ModelMap model)
	{
		ResultInfos resultInfos = new ResultInfos();
		model.addAttribute(RequestNameConstants.RESULT_INFO, resultInfos);
		try
		{
			String systemId = request.getParameter(RequestNameConstants.SYSTEM_TYPE);
			AdminLoginToken loginToken = (AdminLoginToken) SessionUtils.getObjectAttribute(request,
					SessionNameConstants.ADMIN_LOGIN_TOKEN);
			List<FwDept> corpList = fwDeptBO.findByCorpType("corpType", systemId);
			List<FwRole> roleList = fwRoleBO.findByCorpAndStatus(loginToken.getFwUser().getDeptId(),
					CommonConstants.ROLE_STATUS_1);
			model.addAttribute("corpList", corpList);
			model.addAttribute("roleList", roleList);
			model.addAttribute(RequestNameConstants.SYSTEM_TYPE, systemId);

		} catch (Exception e)
		{
			logger.error("用户新增初始化异常", e);

			// 显示提示信息
			resultInfos.setPromptInfo(ResultConstants.getResultInfo(ResultConstants.QUERY_ERROR));
			resultInfos.setIsBack(false);
			resultInfos.setIsRedirect(true);
			resultInfos.setGotoUrlForward(VIEW_URL);
			return new ModelAndView(UrlConstants.INFORMATION_PAGE);
		}
		return new ModelAndView(ADD_GOTO_URL);
	}

	/**
	 * 保存用户信息
	 * @param request 请求对象
	 * @param sysLoginDTO 用户信息数据传输对象
	 * @param model
	 * @return
	 */
	@RequestMapping(params = "method=saveUser")
	public ModelAndView saveUser(HttpServletRequest request, FwUserDTO fwUserDTO, ModelMap model)
	{
		ResultInfos resultInfos = new ResultInfos();
		model.addAttribute(RequestNameConstants.RESULT_INFO, resultInfos);
		try
		{
			int addFlag = fwUserBO.save(fwUserDTO);

			//判断保存是否成功
			if (addFlag == ResultConstants.INSERT_SUCCEED)
			{
				resultInfos.setPromptInfo(ResultConstants.getResultInfo(addFlag));
				resultInfos.setIsBack(false);
				resultInfos.setIsRedirect(true);
				resultInfos.setGotoUrlForward(ADD_URL + "&systemType=" + fwUserDTO.getSystemType());
			} else if (addFlag == CommonConstants.USER_IS_EXIST)
			{
				resultInfos.setPromptInfo("用户帐号重复");
				resultInfos.setIsBack(true);
			} else if (addFlag == CommonConstants.USER_TYPE_1_CORP_IS_EXIST)
			{
				resultInfos.setPromptInfo("同一单位下只能存在一名系统管理员");
				resultInfos.setIsBack(true);
			}
		} catch (Exception e)
		{
			logger.error("用户新增异常", e);

			// 显示提示信息
			resultInfos.setPromptInfo(ResultConstants.getResultInfo(ResultConstants.INSERT_ERROR));
			resultInfos.setIsBack(false);
			resultInfos.setIsRedirect(true);
			resultInfos.setGotoUrlForward(ADD_URL + "&systemType=" + fwUserDTO.getSystemType());
			return new ModelAndView(UrlConstants.INFORMATION_PAGE);
		}
		return new ModelAndView(UrlConstants.INFORMATION_PAGE);
	}

	/**
	 * 用户修改初始化页面
	 * @param request 请求对象
	 * @param loginId　用户ID
	 * @param modeld
	 * @return
	 */
	@RequestMapping(params = "method=editUser")
	public ModelAndView editUser(HttpServletRequest request, Integer loginId, ModelMap model)
	{
		ResultInfos resultInfos = new ResultInfos();
		model.addAttribute(RequestNameConstants.RESULT_INFO, resultInfos);
		try
		{
			FwUserDTO fwUserDTO = fwUserBO.findById(loginId);
			model.addAttribute(RequestNameConstants.RESULT_OBJECT, fwUserDTO);

			String systemId = request.getParameter(RequestNameConstants.SYSTEM_TYPE);
			AdminLoginToken loginToken = (AdminLoginToken) SessionUtils.getObjectAttribute(request,
					SessionNameConstants.ADMIN_LOGIN_TOKEN);
			List<FwDept> corpList = fwDeptBO.findByCorpType("corpType", systemId);
			List<FwRole> roleList = fwRoleBO.findByCorpAndStatus(loginToken.getFwUser().getDeptId(),
					CommonConstants.ROLE_STATUS_1);
			model.addAttribute("corpList", corpList);
			model.addAttribute("roleList", roleList);
			model.addAttribute(RequestNameConstants.SYSTEM_TYPE, systemId);
		} catch (Exception e)
		{
			logger.error("用户编辑异常", e);

			// 显示提示信息
			resultInfos.setPromptInfo(ResultConstants.getResultInfo(ResultConstants.EDIT_ERROR));
			resultInfos.setIsBack(false);
			resultInfos.setIsRedirect(true);
			resultInfos.setGotoUrlForward(VIEW_URL);
			return new ModelAndView(UrlConstants.INFORMATION_PAGE);
		}
		return new ModelAndView(EDIT_GOTO_URL);
	}

	/**
	 * 修改用户信息
	 * @param sysLoginDTO　用户信息数据传输对象
	 * @param model
	 * @return
	 */
	@RequestMapping(params = "method=updUser")
	public ModelAndView updUser(FwUserDTO fwUserDTO, ModelMap model)
	{
		ResultInfos resultInfos = new ResultInfos();
		model.addAttribute(RequestNameConstants.RESULT_INFO, resultInfos);
		try
		{
			int updFlag = fwUserBO.update(fwUserDTO);

			//判断修改是否成功
			if (updFlag == ResultConstants.UPDATE_SUCCEED)
			{
				resultInfos.setPromptInfo(ResultConstants.getResultInfo(updFlag));
				resultInfos.setIsBack(false);
				resultInfos.setIsRedirect(true);
				resultInfos.setGotoUrlForward(VIEW_URL + "&systemType=" + fwUserDTO.getSystemType());
			} else
			{
				resultInfos.setIsBack(true);
			}
		} catch (Exception e)
		{
			logger.error("用户修改异常", e);

			// 显示提示信息
			resultInfos.setPromptInfo(ResultConstants.getResultInfo(ResultConstants.UPDATE_ERROR));
			resultInfos.setIsBack(false);
			resultInfos.setIsRedirect(true);
			resultInfos.setGotoUrlForward(EDIT_URL + "&loginId=" + fwUserDTO.getUserId() + "&systemType="
					+ fwUserDTO.getSystemType());
			return new ModelAndView(UrlConstants.INFORMATION_PAGE);
		}
		return new ModelAndView(UrlConstants.INFORMATION_PAGE);
	}

	/**
	 * 按照用户ID删除角色信息
	 * @param request　请求对象
	 * @param loginId　用户ID
	 * @param modeld
	 * @return
	 */
	@RequestMapping(params = "method=delUser")
	public ModelAndView delUser(HttpServletRequest request, Integer loginId, ModelMap model)
	{
		ResultInfos resultInfos = new ResultInfos();
		model.addAttribute(RequestNameConstants.RESULT_INFO, resultInfos);
		String systemId = request.getParameter(RequestNameConstants.SYSTEM_TYPE);
		try
		{
			int delFlag = fwUserBO.deleteById(loginId);

			//判断删除是否成功
			if (delFlag == ResultConstants.DELETE_SUCCEED)
			{
				resultInfos.setPromptInfo(ResultConstants.getResultInfo(delFlag));
				resultInfos.setIsBack(false);
				resultInfos.setIsRedirect(true);
				resultInfos.setGotoUrlForward(VIEW_URL + "&systemType=" + systemId);
			} else
			{
				resultInfos.setPromptInfo(ResultConstants.getResultInfo(ResultConstants.DELETE_FAILED));
				resultInfos.setIsBack(true);
			}
			model.addAttribute(RequestNameConstants.SYSTEM_TYPE, systemId);
		} catch (Exception e)
		{
			logger.error("用户删除异常", e);

			// 显示提示信息
			resultInfos.setPromptInfo(ResultConstants.getResultInfo(ResultConstants.DELETE_ERR0R));
			resultInfos.setIsBack(false);
			resultInfos.setIsRedirect(true);
			resultInfos.setGotoUrlForward(VIEW_URL + "&systemType=" + systemId);
			return new ModelAndView(UrlConstants.INFORMATION_PAGE);
		}
		return new ModelAndView(UrlConstants.INFORMATION_PAGE);
	}

	/**
	 * 批量删除用户信息
	 * @param request　请求对象
	 * @param loginIds　用户ID集合
	 * @param modeld
	 * @return
	 */
	@RequestMapping(params = "method=delUsers")
	public ModelAndView delUsers(HttpServletRequest request, Integer[] loginIds, ModelMap model)
	{
		ResultInfos resultInfos = new ResultInfos();
		model.addAttribute(RequestNameConstants.RESULT_INFO, resultInfos);
		String systemId = request.getParameter(RequestNameConstants.SYSTEM_TYPE);
		try
		{
			int delFlag = fwUserBO.deleteByIds(loginIds);

			//判断删除是否成功
			if (delFlag == ResultConstants.DELETE_SUCCEED)
			{
				resultInfos.setPromptInfo(ResultConstants.getResultInfo(delFlag));
				resultInfos.setIsBack(false);
				resultInfos.setIsRedirect(true);
				resultInfos.setGotoUrlForward(VIEW_URL + "&systemType=" + systemId);
			} else
			{
				resultInfos.setPromptInfo(ResultConstants.getResultInfo(ResultConstants.DELETE_FAILED));
				resultInfos.setIsBack(true);
			}
			model.addAttribute(RequestNameConstants.SYSTEM_TYPE, systemId);
		} catch (Exception e)
		{
			logger.error("用户批量删除异常", e);

			// 显示提示信息
			resultInfos.setPromptInfo(ResultConstants.getResultInfo(ResultConstants.DELETE_ERR0R));
			resultInfos.setIsBack(false);
			resultInfos.setIsRedirect(true);
			resultInfos.setGotoUrlForward(VIEW_URL + "&systemType=" + systemId);
			return new ModelAndView(UrlConstants.INFORMATION_PAGE);
		}
		return new ModelAndView(UrlConstants.INFORMATION_PAGE);
	}
}
